package us.wili.dev.common.config;

import com.aliyuncs.http.HttpRequest;
import com.github.qcloudsms.httpclient.HTTPException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

@Configuration
@ConfigurationProperties(prefix = "qcloudsm", ignoreUnknownFields = false)
public class LoginSmsConfig {
    private static final Logger logger = LoggerFactory.getLogger(LoginSmsConfig.class);
    private Integer appid;
    private String appkey;
    private String smsSign;
    private Integer loginBody;
    private Integer updatePwdBody;
    private Integer bindingBody;
    private Integer unbundleBody;
    private Integer consultBody;
    private Integer registerBody;
 /**
     * 发送登录验证码
     * @param phone 手机号
     * @param code 验证码
     * @param expiredAt 有效时长 （分钟）
     * @return
     */
    public boolean loginCodeSms(String phone,String code,int expiredAt){
        String[] params = new String[]{code,expiredAt+""};
        //return aloneSms(phone,params,loginBody);
        return aloneSms(phone,params);
    }
    public boolean aloneSms(String phone,String[] params)  {
        //短信内容
        try {
           String content = URLEncoder.encode("【智玩竞技】您的验证码是"+params[0]+","+params[1]+"分钟后过期", "utf-8");
           // String content = URLEncoder.encode("亲爱的张芳釗同学，别讨论魔兽羁绊了，你的勇哥爸爸bigboss祝你今晚吃鸡"+params[0]+","+params[1]+"后过期", "utf-8");

            //拼接参数
            String postData = "type=send&username=k554269458&password=E10ADC3949BA59ABBE56E057F20F883E&gwid=13402ba&mobile=" + phone + "&message=" + content + "";

            String url = "http://jk.106api.cn/smsUTF8.aspx";

            //发送并把结果赋给result,返回一个XML信息,解析xml 信息判断
            String result = sendPost(url, postData);
        }
        catch (Exception e) {
            e.printStackTrace();
            logger.info("短信发送失败，手机号："+phone+","+",错误信息"+e.getMessage());
            return false;
        }
        return true;

    }
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！"+e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }
    public Integer getLoginBody() {
        return loginBody;
    }

    public void setLoginBody(Integer loginBody) {
        this.loginBody = loginBody;
    }
}

